% ML TAU 2013 final project script
% Alt 4:
% Calculate abs(S1 - S2) and perform SVM based on its L to H PCA
% (L*=3 ; H*=120 ; C*=0.250000 ; t*=3 ; deg*=2)

%base_path = '/home/itay/TAU/IML/final';
%libsvm_path = '/opt/libsvm-3.17/matlab';

% Add path to the libsvm
%addpath(base_path);
%addpath(libsvm_path);

function gogo_alt4()


    %
    % Initialization
    %
    close all; clear; clc; tic;
    
    if ~exist('dataforproject.mat','file')
        error('Data file not found in current directory')
    end;
    
    % Saving memory. Loading vars on a need to load basis only throughout
    load('dataforproject.mat','X1train','X2train','gidtrain','ytrain');
    fprintf('Training Data successfully loaded\n');

    X_DELTA = abs(X1train-X2train);

    [m,n] = size(X_DELTA);
    
     X_NORM = X_DELTA/max(X_DELTA(:));

    [X_PC COEFF SCORE latent ] = basic_PCA(X_NORM', n, 'X1train-X2train');
    
    L=3 ; H=120 ; c=0.250000 ; t=3 ; d=2;
    
    acc = do_svm(L,H,t,c,d);
    
    disp(acc);
    

function [accuracy] = do_svm(L,H,t,c,d)

    TX = X_PC(:,L:H);
    
    accuracy = 0.0;

    for k=1:3

        % Training set
        X = TX((gidtrain~=k),:);

        y = ytrain(gidtrain~=k);

        % Create the model
        model = svmtrain(y,X,sprintf('-t %d -g  1.0 -c %f -d %d -h 0 -q',t,c,d));

        % Testing set
        X = TX((gidtrain==k),:);
        y = ytrain(gidtrain==k);

        % Predict
        predictedY = svmpredict(y,X,model);

        results = sum(y.*predictedY > 0) / size(y,1); 
        
        accuracy = accuracy+results;

        %fprintf('Results: %f\n', results);
    end
    
    accuracy = accuracy/3;
end


end

